//
//  AddAddressViewController.m
//  NOTECHN
//
//  Created by yaoqi yaoqi on 2017/5/9.
//  Copyright © 2017年 com.notechn.aoding. All rights reserved.
//

#import "AddAddressViewController.h"
#define LableHight 49
//#import "GoodsAddressManageViewController.h"
@interface AddAddressViewController ()<UITextFieldDelegate>{
    NSString *str;
    UIButton *selectBtn,*unselectBtn;
    NSNumber *reviceSexBtnMessage;
}
@property(nonatomic,strong)UITextField *receiveNameField;
@property(nonatomic,strong)UITextField *receiveAddressField;
@property(nonatomic,strong)UITextField *phoneNumberField;
@property(nonatomic,strong)NSMutableArray *regionIdMuArray;
@property(nonatomic,strong)UILabel *addressLable;
@property(nonatomic,strong)UITextField *zipField;
@end

@implementation AddAddressViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    [self setNavTitleText:@"添加收货地址"];
    [self content];
    [self initLeftViewButton];
}
-(void)initLeftViewButton{
    UIButton *addAddressBtn = [[UIButton alloc]initWithFrame:CGRectMake(KScreenWidth - 54 - 10,Height_StatusBar, 54,44)];
    [addAddressBtn setTitle:@"保存" forState:0];
    [addAddressBtn setTitleColor:rgb_51 forState:0];
    addAddressBtn.titleLabel.font = MediumFont(15);
    [addAddressBtn addTarget:self action:@selector(commintNewAddress:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:addAddressBtn];
    
}
-(void)content{
    reviceSexBtnMessage=[NSNumber numberWithInteger:1];
    float space = 15;
    float lableW = 62 + 15;
    //整体view
    UIView * contentV = [[UIView alloc]init];
    contentV.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:contentV];
    //收货人
    UILabel *nickName=[[UILabel alloc]init];
//    nickName.textAlignment = NSTextAlignmentRight;
    nickName.font=MediumFont(14);
    nickName.text=@"姓名";
    nickName.frame=CGRectMake(space, 0, lableW,LableHight);
    [contentV addSubview:nickName];
    self.receiveNameField=[[UITextField alloc]initWithFrame:CGRectMake(nickName.right+10, 0, KScreenWidth-nickName.right-30, LableHight)];
    self.receiveNameField.placeholder=@"请输入收货人";
    self.receiveNameField.font=MediumFont(14);
    self.receiveNameField.textAlignment = NSTextAlignmentRight;
    [contentV addSubview:self.receiveNameField];
    //线
    UIView *view=[[UIView alloc]initWithFrame:CGRectMake(15, nickName.bottom, KScreenWidth - 30,0.5)];
    view.backgroundColor=rgb(232,232,232);
    [contentV addSubview:view];
    //联系电话
    UILabel *nickName2=[[UILabel alloc]init];
//    nickName2.textAlignment = NSTextAlignmentRight;
    nickName2.font=MediumFont(14);
    nickName2.text=@"手机号";
    nickName2.frame=CGRectMake(space, view.bottom, lableW,LableHight);
    [contentV addSubview:nickName2];
    self.phoneNumberField=[[UITextField alloc]initWithFrame:CGRectMake(nickName2.right+10, view.bottom, KScreenWidth-nickName2.right-30, LableHight)];
    self.phoneNumberField.placeholder=@"请输入联系电话";
    self.phoneNumberField.textAlignment = NSTextAlignmentRight;
    self.phoneNumberField.font=MediumFont(14);
    self.phoneNumberField.keyboardType=UIKeyboardTypeNumberPad;
    [contentV addSubview:self.phoneNumberField];
    //添加一个view
    UIView *view2=[[UIView alloc]initWithFrame:CGRectMake(space, self.phoneNumberField.bottom, KScreenWidth-space*2,0.5)];
    view2.backgroundColor=rgb(232,232,232);;
    [contentV addSubview:view2];
    //邮政
    UILabel *zipLable=[[UILabel alloc]init];
//    zipLable.textAlignment = NSTextAlignmentRight;
    zipLable.font=MediumFont(14);
    zipLable.text=@"邮编";
    zipLable.frame=CGRectMake(space, view2.bottom, lableW,LableHight);
    [contentV addSubview:zipLable];
    self.zipField = [[UITextField alloc]initWithFrame:CGRectMake(zipLable.right+10, view2.bottom, KScreenWidth-zipLable.right-30, LableHight)];
    self.zipField.placeholder=@"请输入邮编号码";
    self.zipField.font=MediumFont(14);
    self.zipField.textAlignment = NSTextAlignmentRight;
    self.zipField.keyboardType=UIKeyboardTypeNumberPad;
    [contentV addSubview:self.zipField];
    
    //添加一个view
    UIView *zipView=[[UIView alloc]initWithFrame:CGRectMake(space, self.zipField.bottom, KScreenWidth-space*2,0.5)];
    zipView.backgroundColor=rgb(232,232,232);
    [contentV addSubview:zipView];
    //省市区
    UIView *view5=[[UIView alloc]initWithFrame:CGRectMake(0, zipView.bottom, KScreenWidth, LableHight)];
    view5.backgroundColor=[UIColor whiteColor];
    [contentV addSubview:view5];
    UILabel *lable3=[[UILabel alloc]init];
//    lable3.textAlignment = NSTextAlignmentRight;
    lable3.font=MediumFont(14);
    lable3.text=@"收货地址";
    lable3.frame=CGRectMake(space, 0, lableW, LableHight);
    [view5 addSubview:lable3];
    self.addressLable=[[UILabel alloc]initWithFrame:CGRectMake(lable3.right+10, 0, KScreenWidth-lable3.right-40, LableHight)];
    self.addressLable.font=MediumFont(14);
    self.addressLable.text = @"请选择省市区";
    self.addressLable.textColor = rgb_153;
    self.addressLable.textAlignment = NSTextAlignmentRight;
    self.addressLable.userInteractionEnabled=YES;
    
    UIImageView *imgv = [[UIImageView alloc]initWithFrame:CGRectMake(KScreenWidth - 15 - 7, self.addressLable.yv, 7, 12)];
    imgv.image= [UIImage imageNamed:@"set_icon_1"];
    [view5 addSubview:imgv];
    imgv.centerYv = self.addressLable.centerYv;
    
    
    
    
    //给lable添加一个手势用于选项地址
    UITapGestureRecognizer *tap=[[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(clickedHandleChooseAddress)];
    [self.addressLable addGestureRecognizer:tap];
    [view5 addSubview:self.addressLable];
    //添加一个view
    UIView *view3=[[UIView alloc]initWithFrame:CGRectMake(space, view5.bottom, KScreenWidth-space*2,.5f)];
    view3.backgroundColor=rgb(232,232,232);
    [contentV addSubview:view3];
    //收货地址
    UILabel *nickName1=[[UILabel alloc]init];
//    nickName1.textAlignment = NSTextAlignmentRight;
    nickName1.font=MediumFont(14);
    nickName1.text=@"详细地址";
    nickName1.frame=CGRectMake(space, view3.bottom, lableW,LableHight);
    [contentV addSubview:nickName1];
    self.receiveAddressField=[[UITextField alloc]initWithFrame:CGRectMake(nickName1.right+10, view3.bottom, KScreenWidth-nickName1.right-30, LableHight)];
    self.receiveAddressField.placeholder=@"请输入详细地址";
    self.receiveAddressField.textAlignment = NSTextAlignmentRight;
    self.receiveAddressField.font=MediumFont(14);
    [contentV addSubview:self.receiveAddressField];
    //添加一个view
    UIView *view4=[[UIView alloc]initWithFrame:CGRectMake(space, nickName1.bottom, KScreenWidth-space*2,0.5)];
    view4.backgroundColor=rgb(232,232,232);
    [contentV addSubview:view4];
    
    selectBtn=[[UIButton alloc]initWithFrame:CGRectMake(15, view4.bottom + 15, 20, 20)];
    [contentV addSubview:selectBtn];
    [selectBtn setImage:[UIImage imageNamed:@"register_icon_4"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"register_icon_5"] forState:UIControlStateSelected];
    [selectBtn addTarget:self action:@selector(selectorSex:) forControlEvents:UIControlEventTouchUpInside];
    
    
    //默认地址设置
    UILabel *setAddress=[[UILabel alloc]init];
//    setAddress.textAlignment = NSTextAlignmentRight;
    setAddress.font=MediumFont(14);
    setAddress.text=@"默认地址";
    setAddress.frame=CGRectMake(selectBtn.right + 5, selectBtn.yv, 80,20);
    [contentV addSubview:setAddress];
    //是/否 按钮

    contentV.frame = CGRectMake(0, Height_NavBar + 10, KScreenWidth, setAddress.bottom);
    
    UIButton *OKBtn=[[UIButton alloc]initWithFrame:CGRectMake(50,KScreenHeight - Height_Indicator - 44 - 60 , KScreenWidth - 100, 44)];
    [OKBtn setTitle:@"保存" forState:UIControlStateNormal];
    OKBtn.titleLabel.font=MediumFont(18);
    [OKBtn setBackgroundImage:[UIImage imageNamed:@"affirm_img_2"] forState:0];
    OKBtn.layer.cornerRadius=5;
    OKBtn.layer.masksToBounds=YES;
    [OKBtn addTarget:self action:@selector(commintNewAddress:) forControlEvents:UIControlEventTouchUpInside ];
    [self.view addSubview:OKBtn];
}
#pragma mark- 是/否按钮点击事件
-(void)selectorSex:(UIButton*)button{
    selectBtn.selected=!button.selected;
    
}
#pragma mark- 省市区手势事件
-(void)clickedHandleChooseAddress{
    [self.phoneNumberField resignFirstResponder];
    [self.receiveNameField resignFirstResponder];
    [self.receiveAddressField resignFirstResponder];
//    DizhiChoseView *dizhiChoseView = [[DizhiChoseView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight)];
//    dizhiChoseView.delegate =self;
//    [self.view addSubview:dizhiChoseView];
}
#pragma mark - addressChooseDelegate
-(void)choseEndClicked:(NSArray*)ary{
    str = @"";
    str=[NSString stringWithFormat:@"%@%@%@",ary[0][@"regionName"],ary[1][@"regionName"],ary[2][@"regionName"]];
    self.addressLable.text = [NSString stringWithFormat:@"%@ %@ %@",ary[0][@"regionName"],ary[1][@"regionName"],ary[2][@"regionName"]];
    [self.regionIdMuArray addObject:ary[0][@"regionId"]];
    [self.regionIdMuArray addObject:ary[1][@"regionId"]];
    [self.regionIdMuArray addObject:ary[2][@"regionId"]];
}
#pragma mark- 点击确认按钮
-(void)commintNewAddress:(UIButton *)button{
    if ((self.receiveNameField.text.length==0)||(self.phoneNumberField.text.length==0)||(self.receiveAddressField.text.length==0)||(self.addressLable.text.length==0)) {
//        [[iToast makeText:@"请完善您的信息"] show];
    }else{
            //检验手机号是否正确
            NSString *regex = @"^1[3|4|5|7|8][0-9]\\d{8}$";
            NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
            if (![predicate evaluateWithObject:self.phoneNumberField.text]) {
//                [[iToast makeText:@"手机号有误，请检查重试!"] show];
                return;
            }
        
    //输入框都不为空时,进行地址修改
        NSMutableDictionary *parameterDict = [NSMutableDictionary dictionary];
        if(self.regionIdMuArray.count != 0) {
            NSLog(@"%@",self.regionIdMuArray);
            [parameterDict setObject:[self.regionIdMuArray objectAtIndex:0] forKey:@"provinceId"];
            [parameterDict setObject:[self.regionIdMuArray objectAtIndex:1] forKey:@"cityId"];
            [parameterDict setObject:[self.regionIdMuArray objectAtIndex:2] forKey:@"districtId"];
        }
        [parameterDict setObject:self.receiveNameField.text forKey:@"consignee"];
        [parameterDict setObject:self.phoneNumberField.text forKey:@"phone"];
        //详细地址
        [parameterDict setObject:self.receiveAddressField.text forKey:@"detailedAddress"];
        //地址拼接
        str=[NSString stringWithFormat:@"%@%@",str,self.receiveAddressField.text];
        [parameterDict setObject:reviceSexBtnMessage forKey:@"isDefault"];
        //提交fullAddress
        [parameterDict setObject:str forKey:@"fullAddress"];
        
        
    }
}
#pragma mark- 懒加载
-(NSMutableArray *)regionIdMuArray{
    if (!_regionIdMuArray) {
        _regionIdMuArray=[NSMutableArray arrayWithCapacity:30];
    }
    return _regionIdMuArray;
}
@end
